package com.koalac.ironman.widget;

import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.koalac.ironman.net.pojo.request.API;
import com.koalac.ironman.util.BitmapUtil;
import com.koalac.ironman.util.StringUtils;
import com.koalac.ironman.widget.ImageLoadAgreement.ImageType;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class LifeqImageView extends ImageView {
	DisplayImageOptions displayImageOptions = ImageLoader.changeDisplayImageOptions();

	public LifeqImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public LifeqImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public LifeqImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 加载图片(显示默认图片)
	 * @param fileName 图片名称
	 * @param imageType 加载图片大小
	 * @param loadListener//加载效果动画
	 * @param progressListener//进度监听
	 */
	public void loadFile(String fileName, ImageType imageType,ImageLoadingListener loadListener,ImageLoadingProgressListener progressListener) {
		if (fileName == null || "".equals(fileName))return;
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(this.getFilePath(fileName, imageType), this,ImageLoader.getDisplayImageOptions(true), 
				loadListener==null?new AnimateFirstDisplayListener():loadListener,progressListener); 
	}
	public void loadCircleImage(String fileName, ImageType imageType) {
	    if (fileName == null || "".equals(fileName))return;
	    ImageLoader imageLoader = ImageLoader.getInstance();
	    imageLoader.displayImage(this.getFilePath(fileName, imageType), this,ImageLoader.getCircleDisplayImageOptions()); 
	}
	public void loadRoundImage(String fileName, ImageType imageType) {
		if (fileName == null || "".equals(fileName))return;
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(this.getFilePath(fileName, imageType), this,ImageLoader.getRoundDisplayImageOptions()); 
	}
	
	/**
	 * 加载图片(不显示默认图片)
	 * @param fileName 图片名称
	 * @param imageType 加载图片大小
	 * @param loadListener//加载效果动画
	 * @param progressListener//进度监听
	 */
	public void loadFileNoneDefImg(String fileName, ImageType imageType,ImageLoadingListener loadListener,ImageLoadingProgressListener progressListener) {
		if (fileName == null || "".equals(fileName))return;
		ImageLoader imageLoader = ImageLoader.getInstance();
		imageLoader.displayImage(this.getFilePath(fileName, imageType), this,ImageLoader.getDisplayImageOptions(false), 
				loadListener==null?new AnimateFirstDisplayListener():loadListener,progressListener); 
	}
	
	/**
	 * 加载图片（显示本地:bitmap）
	 * @param filename
	 * @param imageType
	 * @param loadListener
	 */
	public void displayFile(String filePath, ImageType imageType,ImageLoadingListener loadListener) {
		ByteArrayOutputStream arrayOutputStream = BitmapUtil
				.compressByCale2Stream(filePath, imageType.getValue(), imageType.getValue(), 100);
		this.setImageBitmap(BitmapUtil.getBitmap(arrayOutputStream));
	}
	
	/**
	 * 根据文件路径生成位图
	 * @param filePath
	 * @param imageType
	 * @param loadListener
	 */
	public void displayLocalFile(String filePath, ImageType imageType,ImageLoadingListener loadListener){
		if (StringUtils.isEmpty(filePath))return;
		ImageLoader imageLoader = ImageLoader.getInstance();
		String imageUrl = Scheme.FILE.wrap(filePath);
		imageLoader.displayImage(imageUrl, this,displayImageOptions, loadListener==null?new AnimateFirstDisplayListener():loadListener); 
	}
	
	


	/**
	 * 组装图片路径
	 * @param fileName
	 * @param imageType
	 * @return
	 */
	public static String getFilePath(String fileName,ImageType imageType){
		if(fileName.startsWith(API.BaseImageUrl) ){
			//兼容全路径图床图片
			Log.i("imageName==", fileName+imageType.getSuffix());
			return fileName + imageType.getSuffix();
		}else if(fileName.startsWith("http://") || fileName.startsWith("https://") ){
			//兼容非标准的图片
			Log.i("imageName==", fileName);
			return fileName ;
		}else{
			//兼容标准图床图片
			Log.i("imageName==", API.BaseImageUrl+fileName+imageType.getSuffix());
			return API.BaseImageUrl+fileName+imageType.getSuffix();
		}
	}
	
	/**
	 * 默认图片显示效果动画
	 * @author admin
	 */
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
